package com.outsider.note.task;

import java.io.File;
import java.util.Date;

import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.CustomServiceApi;
import com.jfinal.weixin.sdk.api.MediaApi;
import com.jfinal.weixin.sdk.api.MediaApi.MediaType;
import com.outsider.note.dao.VoiceNote;
import com.outsider.note.entity.BaseNote;
import com.outsider.util.Const;
import com.outsider.weixin.dao.User;

public class VoiceNoteTimerTask extends NoteTimerTask{
	public VoiceNoteTimerTask(BaseNote note) {
		super(note);
	}

	@Override
	public void run() {
		VoiceNote voiceNote=(VoiceNote)note;
		String openID=User.dao.findById(voiceNote.getUserId()).getOpenId();
		String mediaID=voiceNote.getMEDIAID();
		//长期通知 上传语音后获取mediaID
		if (mediaID==null){
			String path=voiceNote.getFILEPATH();
			File file=new File(path);
			ApiResult result=MediaApi.uploadMedia(MediaType.VOICE, file);
			mediaID=result.getStr("media_id");
		}
		CustomServiceApi.sendVoice(openID, mediaID);
		
	}

	@Override
	public Date getCallDate() {
		return note.getDATE();
	}

}
